Passed
Push — feature/jest ( ec2829...4800aa )
by Florian
07:21
created

getters.js ➔ getPagination   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 16
ccs 6
cts 6
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A getters.js ➔ ... ➔ 0 14 2
1 4
export const getUser = function(state) {
2
	return state.user;
3
};
4
5 4
export const isLoggedIn = function(state) {
6 4
	return state.user !== undefined && state.user !== null;
7
};
8
9 4
export const getConfig = function(state) {
10 2
	return function(key = null) {
11 4
		if (key === null) {
12
			return state.config;
13
		}
14
15 4
		if (typeof key !== 'string') {
16
			throw 'Config key must be a string';
17
		}
18
19
		return state.config[key];
20
	}
21
};
22
23 4
export const isMyComment = function (state) {
24 6
	return function(comment) {
25 8
		if (typeof state.user === 'object' && state.user !== null) {
26
			return state.user.id == comment.user_id;
27
		}
28
29
		return false;
30
	}
31
};
32
33 4
export const getOrderBy = function(state) {
34
	return state.orderBy;
35
};
36
37 4
export const getComments = function(state) {
38
	return state.comments;
39
};
40
41 4
export const hasChildren = function(state) {
42 6
	return function(model, modelId, parentId = null) {
43
		let result = state.comments.filter(function(comment) {
44
			// Has to be weak typed, fucking dynamic types
45 6
			return comment.model == model
46
				&& comment.foreign_key == modelId
47
				&& comment.parent_id == parentId;
48
		});
49
50
		return result.length > 0;
51
	}
52
};
53
54 4
export const hasMore = function(state) {
55 2
	return function(model, modelId, parentId = null) {
56
		let key = model + modelId + parentId;
57
		let pagination = Object.assign({}, state.pagination);
58
59 4
		if (pagination[key] !== undefined) {
60
			return pagination[key].nextPage;
61
		}
62
63
		return false;
64
	}
65
};
66
67 4
export const getCommentsForModel = function(state) {
68 2
	return function(model, modelId, parentId = null) {
69
		let comments = state.comments.filter(function(comment) {
70
			// Has to be weak typed, fucking dynamic types
71 6
			return comment.model == model
72
				&& comment.foreign_key == modelId
73
				&& comment.parent_id == parentId;
74
		});
75
76
		comments.sort((a, b) => {
77 6
			switch (state.orderBy) {
78
				case 'newest':
79
					return a.created < b.created;
80
				case 'oldest':
81
					return a.created > b.created;
82
				default:
83
					return a.created < b.created;
84
			}
85
		});
86
87
		return comments;
88
	}
89
};
90
91 4
export const getPagination = function(state) {
92 4
	return function (model, modelId, parentId = null) {
93 4
		let key = model + modelId + parentId;
94
95 4
		if (state.pagination[key] !== undefined) {
96 2
			return state.pagination[key];
97
		}
98
99 2
		return {
100
			page: 1,
101
			nextPage: false,
102
			prevPage: false,
103
			pageCount: 1
104
		}
105
	}
106
}
107